package com.meepo.framework.design.input;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.DialogFragment;

import com.meepo.framework.R;
import com.meepo.framework.design.helper.Utils;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class InputDialog extends DialogFragment {

    public InputListener inputListener;

    public interface InputListener {
        void onSubmit(@NotNull String inputText);

        void beforeTextChanged(CharSequence s, int start, int count, int after);

        void onTextChanged(CharSequence s, int start, int before, int count);
    }

    private String textHint;

    private EditText mEditText;

    public InputDialog() {
    }


    public InputDialog(String textHint, InputListener inputListener) {//提示文字
        this.textHint = textHint;
        this.inputListener = inputListener;
    }

    @NotNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        final Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        View contentView = View.inflate(getActivity(), R.layout.input_dialog_layout, null);
        dialog.setContentView(contentView);

        dialog.setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();

//        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
//        lp.alpha = 1;
//        lp.dimAmount = 0.5f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);
//        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mEditText = contentView.findViewById(R.id.comment_et);
        mEditText.setHint(textHint);
        final TextView tv_send = contentView.findViewById(R.id.submit_tv);


        GradientDrawable etbg = new GradientDrawable();
        etbg.setColor(requireContext().getResources().getColor(R.color.default_background));
        etbg.setCornerRadius(Utils.dp2px(requireContext(), 3));
        mEditText.setBackground(etbg);

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                inputListener.beforeTextChanged(s, start, count, after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                inputListener.onTextChanged(s, start, before, count);
            }

            @Override
            public void afterTextChanged(Editable s) {
                final int primaryTextColor = requireContext().getResources().getColor(R.color.primary_text_color);
                final int hintTextColor = requireContext().getResources().getColor(R.color.hint_text_color);

                tv_send.setTextColor(s.length() > 0 ? primaryTextColor : hintTextColor);
            }
        });


        tv_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEditText.getText().toString())) {
//                    ToastUtils.getInstance().showToast("输入内容为空奥");
                    Toast.makeText(getActivity(), "输入内容为空", Toast.LENGTH_LONG).show();
                } else {
//                    progressDialog = new ProgressDialog(getActivity());
//                    progressDialog.setCanceledOnTouchOutside(false);
//                    progressDialog.show();
                    inputListener.onSubmit(mEditText.getText().toString());

                    dialog.dismiss();
                }
            }
        });
        mEditText.setFocusable(true);
        mEditText.setFocusableInTouchMode(true);
        mEditText.requestFocus();

        final Handler handler = new Handler();
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        hideSoftkeyBoard();
                    }
                }, 200);

            }
        });
        return dialog;
    }

//    public void hideProgressdialog() {
//        progressDialog.cancel();
//    }


    public void hideSoftkeyBoard() {
        try {
            ((InputMethodManager) Objects.requireNonNull(getActivity()).getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (NullPointerException ignored) {

        }
    }


}